Added an explanation for the way the code is set up.
[lhc/web/wiklou.git] / includes / installer / Update.php
1 <?php
2
3 /*
4 * Class for handling database updates. Roughly based off of updaters.inc, with
5 * a few improvements :)
6 */
7 class Update {
8
9 /**
10 * Array of updates to perform on the database
11 *
12 * @var array
13 */
14 protected $updates = array();
15
16 protected $db;
17
18 protected $updater;
19
20 public function __construct( $db ) {
21 $this->db = $db;
22 switch( $this->db->getType() ) {
23 case 'mysql':
24 $this->updater = new MysqlUpdater();
25 break;
26 case 'sqlite':
27 $this->updater = new SqliteUpdater();
28 break;
29 case 'oracle':
30 $this->updater = new OracleUpdater();
31 break;
32 default:
33 throw new MWException( __METHOD__ . ' called for unsupported $wgDBtype' );
34 }
35 }
36
37 public function doUpdates() {
38 global $IP;
39 require_once( "$IP/maintenance/updaters.inc" );
40 $this->loadUpdates();
41 foreach ( $this->updates as $version => $updates ) {
42 foreach( $updates as $params ) {
43 $func = array_shift( $params );
44 call_user_func_array( $func, $params );
45 flush();
46 }
47 // some updates don't get recorded :(
48 if( $version !== 'always' ) {
49 $this->setAppliedUpdates( $version, $updates );
50 }
51 }
52 }
53
54 protected function loadUpdates() {
55 // If the updatelog table hasn't been upgraded, we can't use the new
56 // style of recording our steps. Run all to be safe
57 if( !$this->canUseNewUpdatelog() ) {
58 $this->updates = $this->updater->getUpdates();
59 } else {
60 foreach( $this->updater->getUpdates() as $version => $updates ) {
61 $appliedUpdates = $this->getAppliedUpdates( $version );
62 if( !$appliedUpdates || $appliedUpdates != $updates ) {
63 $this->updates[ $version ] = $updates;
64 }
65 }
66 }
67 $this->getOldGlobalUpdates();
68 }
69
70 protected function getAppliedUpdates( $version ) {
71 $key = "updatelist-$version";
72 $val = $this->db->selectField( 'updatelog', 'ul_value',
73 array( 'ul_key' => $key ), __METHOD__ );
74 if( !$val ) {
75 return null;
76 } else {
77 return unserialize( $val );
78 }
79 }
80
81 protected function setAppliedUpdates( $version, $updates = array() ) {
82 if( !$this->canUseNewUpdatelog() ) {
83 return;
84 }
85 $key = "updatelist-$version";
86 $this->db->delete( 'updatelog', array( 'ul_key' => $key ), __METHOD__ );
87 $this->db->insert( 'updatelog',
88 array( 'ul_key' => $key, 'ul_value' => serialize( $updates ) ),
89 __METHOD__ );
90 }
91
92 /**
93 * Updatelog was changed in 1.17 to have a ul_value column so we can record
94 * more information about what kind of updates we've done (that's what this
95 * class does). Pre-1.17 wikis won't have this column, and really old wikis
96 * might not even have updatelog at all
97 *
98 * @return boolean
99 */
100 protected function canUseNewUpdatelog() {
101 return $this->db->tableExists( 'updatelog' ) &&
102 $this->db->fieldExists( 'updatelog', 'ul_value' );
103 }
104
105 /**
106 * Before 1.17, we used to handle updates via stuff like $wgUpdates,
107 * $wgExtNewTables/Fields/Indexes. This is nasty :) We refactored a lot
108 * of this in 1.17 but we want to remain back-compatible for awhile. So
109 * load up these old global-based things into our update list. We can't
110 * version these like we do with our core updates, so they have to go
111 * in 'always'
112 */
113 private function getOldGlobalUpdates() {
114 global $wgUpdates, $wgExtNewFields, $wgExtNewTables,
115 $wgExtModifiedFields, $wgExtNewIndexes;
116
117 if( isset( $wgUpdates[ $this->db->getType() ] ) ) {
118 foreach( $wgUpdates[ $this->db->getType() ] as $upd ) {
119 $this->updates['always'][] = $upd;
120 }
121 }
122
123 foreach ( $wgExtNewTables as $tableRecord ) {
124 $this->updates['always'][] = array(
125 'add_table', $tableRecord[0], $tableRecord[1], true
126 );
127 }
128
129 foreach ( $wgExtNewFields as $fieldRecord ) {
130 if ( $fieldRecord[0] != 'user' || $doUser ) {
131 $this->updates['always'][] = array(
132 'add_field', $fieldRecord[0], $fieldRecord[1],
133 $fieldRecord[2], true
134 );
135 }
136 }
137
138 foreach ( $wgExtNewIndexes as $fieldRecord ) {
139 $this->updates['always'][] = array(
140 'add_index', $fieldRecord[0], $fieldRecord[1],
141 $fieldRecord[2], true
142 );
143 }
144
145 foreach ( $wgExtModifiedFields as $fieldRecord ) {
146 $this->updates['always'][] = array(
147 'modify_field', $fieldRecord[0], $fieldRecord[1],
148 $fieldRecord[2], true
149 );
150 }
151 }
152
153 }